package com.wangce.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.wangce.entity.Dim;
import com.wangce.entity.Order;
import com.wangce.entity.Status;
import com.wangce.entity.User;
import com.wangce.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * ClassName: OrderController
 * Description:
 * date: 2021/11/11 19:46
 *
 * @author 凡人
 * @since JDK 1.8
 */
@Controller
public class OrderController {

    @Autowired
    OrderService service;

    @RequestMapping("list")
    public String list(Model model, Dim d, @RequestParam(defaultValue = "1") Integer cur, @RequestParam(defaultValue = "3") Integer size){
        PageInfo<Order> list = service.list(d,cur,size);
        System.out.println(">>>>>>>>>>>>>>>>"+d);
        model.addAttribute("list",list);
        model.addAttribute("d",d);
        model.addAttribute("status", Status.values());
        return "list";
    }

    /**
     * 详情页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("toDetail")
    public String toDetail(Model model,Long id){
        Order order = service.getOrder(id);
        model.addAttribute("order",order);
        return "deatil";
    }

    @RequestMapping("toAdd")
    public String toAdd(HttpServletRequest req){
        //生成单号
        Snowflake snowflake = IdUtil.getSnowflake(31, 31);
        req.setAttribute("orderId",snowflake.nextId());
        return "add";
    }

    /**
     * 下单
     * @param order
     * @return
     */
    @RequestMapping("add")
    public String add(HttpSession session, Order order){
        User user = (User) session.getAttribute("user");
        Integer i = service.add(user,order);
        if(i>0){
            return "redirect:list";
        }
        return "redirect:toAdd";
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("upFile")
    @ResponseBody
    public String upFile(MultipartFile file) throws IOException {
        if(file!=null && !file.isEmpty()){
            String name = file.getOriginalFilename();
            String suffx = name.substring(name.lastIndexOf("."));
            boolean matches = Pattern.matches("(.png|.jpg|.jpeg|.gif)",suffx);
            if(matches){
                String newName = UUID.randomUUID() + suffx;
                file.transferTo(new File("d:/pic",newName));
                return "/pic/"+newName;
            }
        }
        return null;
    }

    @RequestMapping("toDelete")
    @ResponseBody
    public String toDelete(Long id){
        Integer i = service.delete(id);
        if(i>0){
            return "ok";
        }
        return "no";
    }
}
